package cn.myeasyai.service;

import cn.myeasyai.config.FileConfig;
import com.obs.services.ObsClient;
import com.obs.services.model.PutObjectRequest;
import com.obs.services.model.PutObjectResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;

/**
 * 文件上传服务
 *
 * @author fusj
 */
@Service
public class HuaweiObsFileServiceImpl implements UploadFileService {

    private static final Logger LOGGER = LoggerFactory.getLogger(HuaweiObsFileServiceImpl.class);

    @Resource
    private ObsClient obsClient;
    @Resource
    private FileConfig fileConfig;

    @Override
    public String uploadFile(InputStream inputStream, String fileName) {
        PutObjectRequest request = new PutObjectRequest();
        request.setBucketName(fileConfig.getObs().getBucketName());
        request.setObjectKey(fileName+".png");
        request.setInput(inputStream);
        final PutObjectResult putObjectResult = obsClient.putObject(request);
        return putObjectResult.getObjectUrl();
    }

}
